#include "hpolicykithelper.h"

#include "core/polkitqt1-authority.h"


HPolicyKitHelper::HPolicyKitHelper(QObject *parent) : QObject(parent)
{

}

HPolicyKitHelper *HPolicyKitHelper::Instance(QObject *parent)
{
    static HPolicyKitHelper _m(parent);
    return &_m;
}

bool HPolicyKitHelper::checkAuthorization()
{
    bool isbChange = checkAuthorization("com.deepin.deepin-defender.checkAuthentication", getpid());
    return isbChange;
}

bool HPolicyKitHelper::checkAuthorization(const QString &actionId, qint64 applicationPid)
{
    PolkitQt1::Authority::Result result;
    result = PolkitQt1::Authority::instance()->checkAuthorizationSync(actionId, PolkitQt1::UnixProcessSubject(applicationPid), /// 第一个参数是需要验证的action，和规则文件写的保持一致
                                                           PolkitQt1::Authority::AllowUserInteraction);

    // 权限校验完成
    if (result == PolkitQt1::Authority::Yes) {
        return true;
    } else {
        return false;
    }
}

bool HPolicyKitHelper::checkAuthorizationHelp()
{
    bool ret = checkAuthorization();

    // 权限校验完成
    Q_EMIT checkAuthorizationFinished();

    return ret;
}
